window.onhashchange = function (event) {
    event.preventDefault();
    event.stopPropagation();
    if (location.hash.split("#")[1] != undefined)
        RenderContentHtml2(location.hash.split("#")[1], "#content");
    else RenderContentHtml2("/Dashboard/Index", "#content");
};

function SetNewCode(data, code) {
    var codes = [];

    for (var i = 0; i < data.length; i++) {
        codes[i] = parseInt(eval('data[i].' + code));
    }

    return Math.max.apply(Math, codes) + 1;
}

function RenderContentHtml(url, container) {
    $.get(url, function (data) {
        $(container).empty().html(data);
    });
}

function RenderContentHtmlPost(url, params, container) {
    $.post(url, params, function(data) {
        $(container).empty().html(data);
    });
}

function RenderContentHtml2(url, container) {
    window.location.hash = url;
}

// This object contains Ajax's properties to be used in AjaxActionGet and AjaxActionPost methods
function ObjAjax() {
    this.data = null;
    this.textStatus = null;
    this.jqXHR = null;
    this.errorThrown = null;
}

function AjaxActionGet(url) {
    obj = new ObjAjax();

    $.ajax({
        type: "GET",
        url: url,
        async: false
    }).done(function (data, textStatus, jqXHR) {
        obj.data = data;
        obj.textStatus = textStatus;
        obj.jqXHR = jqXHR;
    }).fail(function (jqXHR, textStatus, errorThrown) {
        obj.textStatus = textStatus;
        obj.jqXHR = jqXHR;
        obj.errorThrown = errorThrown;
    });

    return obj.data;
}

function AjaxParamsActionPost(url, params) {
    obj = new ObjAjax();

    $.ajax({
        type: "POST",
        url: url,
        data: params,
        async: false
    }).done(function (data, textStatus, jqXHR) {
        obj.data = data;
        obj.textStatus = textStatus;
        obj.jqXHR = jqXHR;
    }).fail(function (jqXHR, textStatus, errorThrown) {
        obj.textStatus = textStatus;
        obj.jqXHR = jqXHR;
        obj.errorThrown = errorThrown;
    });

    return obj.data;
}

function AjaxModelActionPost(url, jsonObject) {
    obj = new ObjAjax();

    $.ajax({
        type: "POST",
        url: url,
        data: { model: jsonObject },
        async: false
    }).done(function (data, textStatus, jqXHR) {
        obj.data = data;
        obj.textStatus = textStatus;
        obj.jqXHR = jqXHR;
    }).fail(function (jqXHR, textStatus, errorThrown) {
        obj.textStatus = textStatus;
        obj.jqXHR = jqXHR;
        obj.errorThrown = errorThrown;
    });

    return obj.data;
}

function GetLargeDate() {
    var meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
    var diasSemana = new Array("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado");
    var f = new Date();

    return diasSemana[f.getDay()] + ", " + f.getDate() + " de " + meses[f.getMonth()] + " de " + f.getFullYear();
}

function GetShortDate() {
    var f = new Date();

    return ('0' + f.getDate()).slice(-2).toString()
            + '/' + ('0' + (f.getMonth() + 1)).slice(-2).toString()
            + '/' + f.getFullYear().toString();
}

function GetShortDate(date) {
    try {
        return ('0' + date.getDate()).slice(-2).toString()
            + '/' + ('0' + (date.getMonth() + 1)).slice(-2).toString()
            + '/' + date.getFullYear().toString();
    }
    catch (e) {
        return null;
    }
}

function GetShortJSONDate(d) {
    try {
        date = new Date(parseInt(d.slice(6, -2)));

        return ('0' + date.getDate()).slice(-2).toString()
            + '/' + ('0' + (date.getMonth() + 1)).slice(-2).toString()
            + '/' + date.getFullYear().toString();
    }
    catch (e) {
        return null;
    }
}